package com.ftg.examsys.service;

import com.ftg.examsys.mapper.UserMapper;
import com.ftg.examsys.mapper.UserNoMapper;
import com.ftg.examsys.mapper.UserRoleMapper;
import com.ftg.examsys.util.ConstMess;
import com.ftg.examsys.util.HutoolUtilForMe;
import com.ftg.examsys.util.PageEx;
import com.ftg.examsys.vo.UserDetailVo;
import com.ftg.examsys.vo.UserInfoVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.List;

/**
 * 用户业务逻辑处理
 *
 * @author kangjx
 * @version 1.0
 * @date 2023/9/7 上午11:01
 */
@Service
public class UserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private UserNoMapper userNoMapper;

    @Resource
    private UserRoleMapper userRoleMapper;

    /**
     * 登陆 功能
     *
     * @param username 用户 名
     * @param password 密码
     * @return
     */
    public UserInfoVo login(String username, String password) {
        if (username == null || password == null) {
            throw new RuntimeException("用户名、密码不能为空!!");
        }

        String reg = "^[0-9a-zA-Z]{8,16}$";

        if (!username.matches(reg)) {
            throw new RuntimeException("用户名只能为数字、字母组成 8-16");
        }

        if (!password.matches(reg)) {
            throw new RuntimeException("密码只能为数字、字母组成 8-16");
        }

        password = HutoolUtilForMe.jiaMi(password);
        return userMapper.checkUser(username, password);
    }

    /**
     * 展示
     *
     * @param username 用户
     * @param page     第几嶡
     * @param limit    显示
     * @return
     */
    public PageEx<UserDetailVo> showUserAll(String username, int page, int limit) {

        PageEx<UserDetailVo> pageEx = new PageEx<>(page,limit);

        int reports = userMapper.showUserCount(username);
        pageEx.setTotal(reports);

        List<UserDetailVo> rows = userMapper.showUserDetail(username, (page - 1) * limit, limit);
        pageEx.setRows(rows);

        return pageEx;
    }

    @Transactional(rollbackFor = SQLException.class)
    public int saveUser(String username, String realname, byte gender, String number, List<Integer> roleIds) {

        String mima = HutoolUtilForMe.jiaMi(ConstMess.DEFAULT_PASSWORD);


        if (userNoMapper.findoByNumber(number) != null) {
            throw new RuntimeException("对不起当前学号已存在了！！！");
        }

        userNoMapper.save(number);

        int userNo = userNoMapper.findoByNumber(number);

        userMapper.save(username, mima, realname, gender, userNo);

        // 查询一下 username
        int userid = userMapper.findByUsername(username);

        for (int i = 0; i < roleIds.size(); i++) {
            userRoleMapper.saveUserRole(userid, roleIds.get(i));
        }
        return 1;
    }

    public UserDetailVo oneById(int id) {
        return userMapper.oneById(id);
    }

    public List<Integer> oneAllRoleById(int id) {
        return userMapper.oneAllRoleById(id);
    }

    @Transactional(rollbackFor = SQLException.class)
    public boolean modifyUser(List<Integer> roleids, UserDetailVo udv) {

        // 修改用户编号表
        userMapper.modifyUserNoById(udv.getNumber(),udv.getStatus(),udv.getId());

        //修改用户表
        userMapper.modifyUserById(udv);

        //用户角色中间表
        //1 先删除所有 11
        //2 重新添加一次
        userMapper.deleteRoleByUserId(udv.getId());
        for (Integer roleid : roleids) {
            userMapper.insertRole(udv.getId(),roleid);
        }
        return true;
    }

    /**
     * 根据用户主键进行删除操作
     * @param id  用户主键
     * @return    影响行数
     */
    @Transactional(rollbackFor = SQLException.class)
    public boolean delById(Integer id) {

        userRoleMapper.deleteByUserId(id);

        int usernoid = userMapper.findUserNoIdById(id);

        userMapper.delUserById(id);

        userNoMapper.deleteById(usernoid);

        return true;
    }
}
